<?php

/**
 * Description of Social
 * 
 * @author anhducbkhn
 * @since Nov 12, 2013 - 5:57:36 PM
 */
Yii::import('ext.hoauth.hybridauth.Hybrid.Hybrid_Auth');

class Social
{

    const GOOGLE   = 'Google';
    const FACEBOOK = 'Facebook';
    const TWITTER  = 'Twitter';

    public $session = '';
    public $sosial  = NULL;

    /**
     * Singleton install
     * @param type $provider
     * @return Adapter
     */
    public function getInstanceSocial($provider)
    {

        if ($this->sosial == NULL)
        {
            $config = Yii::app()->params['hoauth'];

            $hybirth = new Hybrid_Auth($config);
            return $hybirth->getAdapter($provider);
        }
        else
        {
            return $this->sosial;
        }
    }

    /**
     * 
     * @param type $provider
     * @return boolean 
     */
    public static function isConnect($provider)
    {
        $social   = new Social();
        $provider = $social->getInstanceSocial($provider);
        return $provider->isUserConnected();
    }

    /*
     * @return social session of current user
     */

    public static function getSession($provider)
    {
        $social   = new Social();
        $provider = $social->getInstanceSocial($provider);
        return json_encode($provider->getAccessToken());
    }

    public static function GSync($appoint)
    {

//        Yii::import('ext.googleapi.lib.Google_Client', true);
//        Yii::import('ext.googleapi.lib.contrib.Google_CalendarService', true);
        $g_client  = Yii::app()->googleapi->instanceClient;
        $g_service = Yii::app()->googleapi->instanceCalendarService;
//
//        $client = new Google_Client();
//        $client->setApplicationName("webmaster CRM");
//
//        $client->setClientId('848332076147.apps.googleusercontent.com');
//        $client->setClientSecret('CWmBOLwwdAOvDn3uiY_jgqG3');
//        $client->setRedirectUri('http://voithan.com/demo/webmaster/test/t');
//        print_r(self::getSession(self::GOOGLE));die;
//        print_r($g_client->getAccessToken());die;

        $access_token = array(
            'access_token'  => 'ya29.1.AADtN_XqUfuD8TASXGh7vx6_aq_9hRjNGjzK6lT_uZHrdD-cT42zhUlowBEvRBOaAjwsRH1jBw',
            'refresh_token' => '',
            'token_type'    => 'Bearer',
            'expires_in'    => '3597',
            'id_token'      => 'ya29.1.AADtN_XqUfuD8TASXGh7vx6_aq_9hRjNGjzK6lT_uZHrdD-cT42zhUlowBEvRBOaAjwsRH1jBw',
            'created'       => '1385716070'
        );

        $g_client->setAccessToken(CJSON::encode($access_token));
//
        die('zzzzzzzzzzzzzzzzzz');
//        $g_service = Google_CalendarService($client);
//        die('fffllpppoo');

        if (!self::isConnect(Social::GOOGLE))
        {
            return FALSE;
        }
        else
        {
//            die('ffgjj');
            $e_name    = $appoint->service->s_name;
            $e_start   = AppointGoogle::convertToRFC3339($appoint->start_time, $appoint->date);
            $e_end     = AppointGoogle::convertToRFC3339($appoint->end_time, $appoint->date);
            $e_address = $appoint->service->address;

            $event = new Google_Event();
            $event->setSummary($e_name);
            $event->setLocation($e_address);

            $start = new Google_EventDateTime();
            $start->setDateTime($e_start);
            $event->setStart($start);

            $end        = new Google_EventDateTime();
            $end->setDateTime($e_end);
            $event->setEnd($end);
            $list_event = $g_service->events->insert('primary', $event);

            print_r($list_event);
            die;
        }
    }

}

/**
     * End of Social
     */
    
